<?php

namespace app\models;

use Yii;

/**
 * This is the model class for table "user_pet".
 *
 * @property int $puid 主键id
 * @property int $user_id 用户id
 * @property int $pid 宠物id
 * @property int $feed_status 0、未喂食             1、喂食未满             2、喂食已满
 * @property int $auction_status 0、正常             1、挂卖中
 * @property int $ascription_status 0、已转卖             1、正常             2、已冻结
 * @property int $is_fictitious
 * @property int $created_at
 * @property int $updted_at
 */
class UserPet extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'user_pet';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['user_id', 'pid', 'created_at', 'updted_at'], 'integer'],
            [['feed_status','auction_status', 'ascription_status', 'is_fictitious'], 'integer'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'puid' => '主键id',
            'user_id' => '用户id',
            'pid' => '宠物id',
            'feed_status' => '0、未喂食
            1、喂食未满
            2、喂食已满',
            'auction_status' => '0、正常
            1、挂卖中',
            'ascription_status' => '0、已转卖
            1、正常
            2、已冻结',
            'is_fictitious' => 'Is Fictitious',
            'created_at' => 'Created At',
            'updted_at' => 'Updted At',
        ];
    }

    /**
     * @param $userId
     * @param $pid
     * @return array|bool|null|\yii\db\ActiveRecord
     */
    public function getUserPetRow($userId, $pid)
    {
        if (empty($userId) || empty($pid)) {
            return false;
        }
        return $this->find()->where('user_id=:user_id and pid=:pid and ascription_status=1',
            [':user_id' => $userId, ':pid' => $pid])
            ->with('pet')->orderBy('puid', 'desc')->one();
    }

    /**
     * 关联宠物表
     * @return \yii\db\ActiveQuery
     */
    public function getPet(){
        return $this->hasOne(Pet::className(), ['pid' => 'pid']);
    }

    public function getSumFee()
    {
        return (new Feed())->getTodaySumFeed($this->pid);
    }

    /**
     * @return $this
     */
    /**
     * @return $this
     */
    public function getSearchQuery()
    {
        return $this->find()->where('user_pet.ascription_status=1 and user_pet.auction_status=1 and user_pet.price>0')
            ->joinWith('pet');
    }
}
